package dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import orm.ChildrenBehaviorPurpose;
import orm.CollectivityMonthArrange;
import orm.CollectivityWeekArrange;
import orm.CollectivityWeekTimeBucket;
import orm.IndividualWeekArrange;
import util.DBHelper;

public class IWeekArrangeDAO {
	
	public void insertIWeekArrange(Statement statement, 
			ArrayList<IndividualWeekArrange> iwaArr, String iWeekArrangeID) throws SQLException {
		IndividualWeekArrange iwa = null;
		for (int i = 0; i < iwaArr.size(); i++) {
			iwa = iwaArr.get(i);
			statement.execute("insert into individualweekarrange values('" + iWeekArrangeID + "'," +
					iwa.getWeekID() + ", '" + iwa.getEnforceTime() + "' , '" + iwa.getEndEnforceTime() + "', '" + iwa.getRemark() + "')");
		}
	}
	
	public void updateIWeekArrange(Statement statement,
			ArrayList<IndividualWeekArrange> iwaArr, String iWeekArrangeID) throws SQLException {
		IndividualWeekArrange iwa = null;
		for (int i = 0; i < iwaArr.size(); i++) {
			iwa = iwaArr.get(i);

			StringBuffer sqlStr = new StringBuffer();
			sqlStr.append("update individualweekarrange set EnforceTime = '" );
			sqlStr.append(iwa.getEnforceTime() + "', EndEnforceTime = '" + iwa.getEndEnforceTime() + "', Remark = '" + iwa.getRemark());
			sqlStr.append("' where IWeekArrangeID = '" + iwa.getIWeekArrangeID() + "' and WeekID = " + iwa.getWeekID() + "");
			statement.execute(sqlStr.toString());
		}
	}
	
	public void deleteIWeekArrange(Statement statement, String iWeekArrangeID) throws SQLException {
		statement.execute("delete from individualweekarrange where IWeekArrangeID='" + iWeekArrangeID + "'");
	}
	
	public ArrayList<IndividualWeekArrange> getiwaByiwaID(String iWeekArrangeID) throws SQLException {
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		
		ArrayList<IndividualWeekArrange> al = new ArrayList<IndividualWeekArrange>();
		IndividualWeekArrange iwa = null;
		conn = DBHelper.getConnection();
		stmt = conn.createStatement();
		rs = stmt.executeQuery("select * from individualweekarrange where IWeekArrangeID='" + iWeekArrangeID + "'");
		
		while (rs.next()) {
			iwa = new IndividualWeekArrange();
			int i = 1;
			iwa.setIWeekArrangeID(rs.getString(i++));
			iwa.setWeekID(rs.getInt(i++));
			iwa.setEnforceTime(rs.getString(i++));
			iwa.setEndEnforceTime(rs.getString(i++));
			iwa.setRemark(rs.getString(i++));
			al.add(iwa);
		}
		rs.close();
		stmt.close();
		conn.close();
		
		return al;
	}
}
